package com.alipay.controller;


import com.alibaba.fastjson.JSONObject;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.config.AlipayConfig;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

/**
 * @author 阿林
 * @PackageName:com.ali.controller
 * @ClassName:Alipay
 * @Description:这是一个非常哇塞的人写的哇塞的项目！哇塞！
 * @date 2021/1/11 21:57
 */
@RestController
@CrossOrigin
public class Alipay {

    @RequestMapping("/pay/{name}/{price}")
    public void pay(HttpServletResponse response,
                    @PathVariable("name") String name,
                    @PathVariable("price") double price
    ) throws Exception {
        DefaultAlipayClient client = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);
        AlipayTradePagePayRequest alipayTradePagePayRequest = new AlipayTradePagePayRequest();

        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("out_trade_no",System.currentTimeMillis());
        map.put("product_code","FAST_INSTANT_TRADE_PAY");
        map.put("total_amount",price+"");
        map.put("subject",name);
        String string = JSONObject.toJSONString(map);
        System.out.println(string);
        alipayTradePagePayRequest.setBizContent(string);
        String body = client.pageExecute(alipayTradePagePayRequest).getBody();


        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write(body);
        response.getWriter().flush();
        response.getWriter().close();

    }
}
